package cn.wolfcode.education.web.controller;

import cn.wolfcode.education.anno.PermissionName;
import cn.wolfcode.education.domain.Client;
import cn.wolfcode.education.domain.RegularStu;
import cn.wolfcode.education.query.ClientQuery;
import cn.wolfcode.education.service.IClientService;
import cn.wolfcode.education.util.JSONResult;
import cn.wolfcode.education.util.PageResult;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;


@Controller
@RequestMapping("/client")
public class ClientController {

    @Autowired
    private IClientService clientService;

    @RequiresPermissions("client:view")
    @PermissionName("客户主页")
    @RequestMapping("")
    public String list() {
        return "client";
    }


    @RequiresPermissions("client:query")
    @PermissionName("客户列表")
    @RequestMapping("query")
    @ResponseBody
    public PageResult query(@ModelAttribute("qo") ClientQuery qo) {
        return clientService.query(qo);
    }

    @RequiresPermissions("client:save")
    @PermissionName("添加客户")
    @RequestMapping("save")
    @ResponseBody
    public JSONResult save(Client client) {
        JSONResult result = new JSONResult();
        try {
            clientService.save(client);
            return result;
        } catch (Exception e) {
            result.setMsg(e.getMessage());
            return result;
        }
    }

    @RequiresPermissions("client:update")
    @PermissionName("编辑客户")
    @RequestMapping("update")
    @ResponseBody
    public JSONResult update(Client client) {
        JSONResult result = new JSONResult();
        try {
            clientService.update(client);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("程序异常啦");
            return result;
        }
    }

    @RequiresPermissions("client:toPool")
    @PermissionName("客户转入资源池")
    @RequestMapping("toPool")
    @ResponseBody
    public JSONResult toPool(Long clientId) {
        JSONResult result = new JSONResult();
        try {
            clientService.clientToPool(clientId);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("程序异常");
            return result;
        }
    }


    @RequiresPermissions("client:formal")
    @PermissionName("客户入学")
    @RequestMapping("formal")
    @ResponseBody
    public JSONResult formal(RegularStu regularStu) {

        System.out.println(regularStu);

        JSONResult result = new JSONResult();
        try {
            clientService.formal(regularStu);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("程序异常");
            return result;
        }
    }

    @RequiresPermissions("client:removeClient")
    @PermissionName("客户删除")
    @RequestMapping("removeClient")
    @ResponseBody
    public JSONResult removeClient(Long clientId) {

        System.out.println(clientId);

        JSONResult result = new JSONResult();
        try {
            clientService.removeClient(clientId);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("程序异常");
            return result;
        }
    }

    @RequiresPermissions("client:getTestClient")
    @PermissionName("获取所有潜在客户")
    @RequestMapping("getTestClient")
    @ResponseBody
    public List<Client> getTestClient() {
        List<Client> allClient = clientService.getTestClient();
        return allClient;
    }

    @RequiresPermissions("client:isExsists")
    @PermissionName("查询客户是否存在")
    @RequestMapping("isExsists")
    @ResponseBody
    public JSONResult isExsists(String clientName, Long clientId) {
        JSONResult result = new JSONResult();
        if (clientId != null) {
            return result;
        }
        try {
            clientService.isExsists(clientName);
            return result;
        } catch (Exception e) {
            result.setMsg(e.getMessage());
            return result;
        }
    }

    @RequiresPermissions("client:judge")
    @PermissionName("判断客户归属")
    @RequestMapping("judge")
    @ResponseBody
    public JSONResult judge(Long clientId) {
        JSONResult result = new JSONResult();
        try {
            clientService.judge(clientId);
            return result;
        } catch (Exception e) {
            result.setMsg(e.getMessage());
            return result;
        }
    }


    @RequestMapping("export")
    @PermissionName("导出")
    @RequiresPermissions("client:export")
    public void export(ClientQuery qo, HttpServletResponse response) throws Exception {
        JSONResult result = new JSONResult();
        //1.查询出列表数据
        qo.setRows(10000);
        List<Client> clients = clientService.queryForList(qo);

        response.setHeader("Content-Disposition", "attachment;filename=clientloyee.xls");
        OutputStream outputStream = response.getOutputStream();

        //创建一个工作簿,目标为outputStream
        WritableWorkbook workbook = Workbook.createWorkbook(outputStream);

        //处理标题
        //创建一个可以写的sheet
        WritableSheet sheet = workbook.createSheet("clientloyee", 0);

        //创建一个单元格
        Label nameLabel = new Label(0, 0, "客户名");
        Label inputEmployeeLabel = new Label(1, 0, "录入人");
        Label buildDocTimeLabel = new Label(2, 0, "建档时间");
        Label traceNumberLabel = new Label(3, 0, "跟进次数");
        Label lastTraceTimeLabel = new Label(4, 0, "最后跟进时间");
        Label wishLevelLabel = new Label(5, 0, "意向等级");
        Label wishCampusLabel = new Label(6, 0, "意向校区");
        Label wishClassLabel = new Label(7, 0, "意向班级");
        Label wishSubjectLabel = new Label(8, 0, "意向学科");
        Label qqLabel = new Label(9, 0, "qq号码");
        Label telLabel = new Label(10, 0, "电话");
        Label emailLabel = new Label(11, 0, "邮箱");
        Label wechatLabel = new Label(12, 0, "微信");
        Label stateLabel = new Label(13, 0, "状态");
        Label attachEmployeeLabel = new Label(14, 0, "当前跟进人");
        Label remarkLabel = new Label(15, 0, "备注");

        sheet.addCell(nameLabel);
        sheet.addCell(inputEmployeeLabel);
        sheet.addCell(buildDocTimeLabel);
        sheet.addCell(traceNumberLabel);
        sheet.addCell(lastTraceTimeLabel);
        sheet.addCell(wishLevelLabel);
        sheet.addCell(wishCampusLabel);
        sheet.addCell(wishClassLabel);
        sheet.addCell(wishSubjectLabel);
        sheet.addCell(qqLabel);
        sheet.addCell(telLabel);
        sheet.addCell(emailLabel);
        sheet.addCell(wechatLabel);
        sheet.addCell(stateLabel);
        sheet.addCell(attachEmployeeLabel);
        sheet.addCell(remarkLabel);

        //循环迭代list
        for (int i = 0; i < clients.size(); i++) {
            Client client = (Client) clients.get(i);

            int row = i + 1;


            Label tempnameLabel = new Label(0, row, client.getName());
            Label tempinputEmployeeLabel = new Label(1, row, client.getInputEmployee().getUsername());
            Label tempbuildDocTimeLabel = new Label(2, row, client.getBuildDocTime().toLocaleString());
            Label temptraceNumberLabel = new Label(3, row, client.getTraceNumber().toString());
            if (client.getLastTraceTime() != null) {
                Label templastTraceTimeLabel = new Label(4, row, client.getLastTraceTime().toLocaleString());
                sheet.addCell(templastTraceTimeLabel);
            }
            if (client.getWishLevel() != null) {
                Label tempwishLevelLabel = new Label(5, row, client.getWishLevel().getName());
                sheet.addCell(tempwishLevelLabel);
            }
            if (client.getWishCampus() != null) {
                Label tempwishCampusLabel = new Label(6, row, client.getWishCampus().getName());
                sheet.addCell(tempwishCampusLabel);
            }
            if (client.getWishClass() != null) {
                Label tempwishClassLabel = new Label(7, row, client.getWishClass().getName());
                sheet.addCell(tempwishClassLabel);
            }
            if (client.getWishSubject() != null) {
                Label tempwishSubjectLabel = new Label(8, row, client.getWishSubject().getName());
                sheet.addCell(tempwishSubjectLabel);
            }
            if (client.getQq() != null) {
                Label tempqqLabel = new Label(9, row, client.getQq().toString());
                sheet.addCell(tempqqLabel);
            }
            if (client.getTel() != null) {
                Label temptelLabel = new Label(10, row, client.getTel().toString());
                sheet.addCell(temptelLabel);
            }
            if (client.getEmail() != null) {
                Label tempemailLabel = new Label(11, row, client.getEmail());
                sheet.addCell(tempemailLabel);
            }
            if (client.getWechat() != null) {
                Label tempwechatLabel = new Label(12, row, client.getWechat());
                sheet.addCell(tempwechatLabel);
            }
            Label tempstateLabel = new Label(13, row, client.getState().getName());
            if (client.getAttachEmployee() != null) {
                Label tempattachEmployeeLabel = new Label(14, row, client.getAttachEmployee().getUsername());
                sheet.addCell(tempattachEmployeeLabel);
            }
            if (client.getRemark() != null) {
                Label tempremarkLabel = new Label(15, row, client.getRemark());
                sheet.addCell(tempremarkLabel);
            }

            sheet.addCell(tempnameLabel);
            sheet.addCell(tempinputEmployeeLabel);
            sheet.addCell(tempbuildDocTimeLabel);
            sheet.addCell(temptraceNumberLabel);
            sheet.addCell(tempstateLabel);
        }

        workbook.write();
        workbook.close();
    }

    /*@RequestMapping("pie")
    public String pie(Model model) {
        List<Map<String, Object>> list = clientService.queryAllClient();

        // 使用一个list集合将获取到的分组数据封装
        List<Map<String, Object>> result = new ArrayList<>();
        List<Object> groupByType = new ArrayList<>();

        for (Map<String, Object> map : list) {
            // 拿到所有分组名
            groupByType.add(map.get("groupByType"));
            HashMap<String, Object> m = new HashMap<>();
            map.put("name", map.get("groupByType"));
            map.put("value", map.get("clientNum"));
            result.add(m);

            int maxNum = 0;

            int max = (int) map.get("clientNum");

            if (max > maxNum) {
                maxNum = max;
            }
        }

        // 将数据转成JSON格式共享

        return null;
    }*/
}
























